<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath%>">
<!-- 加载引用文件 -->
<%@ include file="../common/verify.jsp"%>
<jsp:include page="../common/meta.jsp"></jsp:include>
<jsp:include page="../common/easyui.jsp"></jsp:include>
<script type="text/javascript">
	var topClassDataGrid;
	var editTopClassWindow;
	var editTopClassForm;
	$(function() {
		console.info('开始加载数据...');
		topClassDataGrid = $('#topClassDataGrid').datagrid({
			url : 'json/topClass_list',
			title : '网址分类',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 10,
			pageList : [10, 20, 30],
			fit : true,
			fitColumns : false,
			nowrap : false,
			border : true,
			idField : 'id',
			columns : [[{
				title : '内部编码',
				field : 'id',
				width : 80
			}, {
				title : '分类名称',
				field : 'name',
				width : 150,
				editor : {
					type : 'validatebox',
					options : {
						required : true
					}
				}
			}, {
				title : '备注',
				field : 'description',
				width : 80
			}]],
			toolbar : [{
				text : '增加',
				iconCls : 'icon-add',
				handler : newtopClass
			}, '-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : removetopClass
			}, '-', {
				text : '修改',
				iconCls : 'icon-edit',
				handler : editTopClass
			}, '-', {
				text : '取消选中',
				iconCls : 'icon-undo',
				handler : function(){
					topClassDataGrid.datagrid('unselectAll');
				}
			}],
			loadMsg : '加载中，请稍候...'
		});

		editTopClassWindow = $('#editTopClassWindow').window({
			closed : true
		});

		editTopClassForm = editTopClassWindow.find('form');
		
		$('#btnSave').linkbutton();
		$('#btnCancel').linkbutton();
	});

	function newtopClass() {
		console.info('增加...');
		editTopClassWindow.window('open');
		editTopClassWindow.find('#topClass_id').attr('readonly', false);
		editTopClassForm.form('clear');
		editTopClassWindow.find('#topClass_id').val(0);
		editTopClassForm.url = 'json/topClass_add';
	}

	function editTopClass() {
		console.info('修改...');

		var row = topClassDataGrid.datagrid('getSelected');
		if (row) {
			editTopClassWindow.window('open');
			editTopClassWindow.find('#topClass_id').attr('readonly', true);
			editTopClassWindow.find('#topClass_id').val(row.id);
			editTopClassWindow.find('#topClass_name').val(row.name);
			editTopClassWindow.find('#topClass_description').val(row.description);
			
			editTopClassForm.url = 'json/topClass_update';
		} else {
			$.messager.alert('提示', '请选择要修改的记录！', 'error');
		}
	}

	function removetopClass() {
		console.info('删除...');
		var classID = [];
		var rows = topClassDataGrid.datagrid('getSelections');
		if (rows.length > 0) {
			$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
				if (r) {
					for ( var i = 0; i < rows.length; i++) {
						classID.push(rows[i].id);
					}
					$.ajax({
						url : 'json/topClass_delete',
						data : {
							id : classID.join(',')[0]
						},
						cache : false,
						dataType : "json",
						success : function(response) {
							topClassDataGrid.datagrid('unselectAll');
							topClassDataGrid.datagrid('reload');
							$.messager.show({
								title : '提示',
								msg : '删除成功！'
							});
						}
					});
				}
			});
		} else {
			$.messager.alert('提示', '请选择要删除的记录！', 'error');
		}
	}

	// 保存新增分类
	function saveTopClass() {
		console.info('保存...');
		if (editTopClassForm.url == '') {
			editTopClassForm.url = 'joso/topClass_add';
		}

		editTopClassForm.form('submit', {
			url : editTopClassForm.url,
			success : function(data) {
				console.info(data);
				editTopClassWindow.window('close');
				topClassDataGrid.datagrid('reload');
				$.messager.show({
					title : '提示',
					msg : '增加成功！'
				});
			},
			error : function(json) {
				console.info('保存出错！');
			}
		});
	}

	function closeWindow() {
		editTopClassWindow.window('close');
	}
</script>
<title>网址分类列表</title>
</head>
<body style="width: 720px; height: 480px; margin: 0 auto;">
<div title="网址分类" style="width: 100%; height: 100%;">
	<table id="topClassDataGrid"></table>
</div>

<div id="editTopClassWindow" title="Top分类"
	style="width: 400px; height: 250px;">
	<div style="padding: 20px 20px 40px 80px;">
		<form method="post">
			<table>
				<tr>
					<td>内部编码：</td>
					<td><input id="topClass_id" name="topClass.id"></input></td>
				</tr>
				<tr>
					<td>分类名称：</td>
					<td><input id="topClass_name" name="topClass.name"></input></td>
				</tr>
				<tr>
					<td>备注：</td>
					<td><input id="topClass_description" name="topClass.description"></input></td>
				</tr>
			</table>
		</form>
	</div>
	<div style="text-align: center; padding: 5px;">
		<a href="javascript:void(0)" onclick="saveTopClass()" id="btnSave"
			icon="icon-save">保存</a> <a href="javascript:void(0)"
			onclick="closeWindow()" id="btnCancel" icon="icon-cancel">取消</a>
	</div>
</div>
</body>
</html>